package com.jpa.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.jpa.entity.ExcelDto;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;

import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSONObject;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;

/**
 * @Author huyunqiang
 * @Description
 * @Date 2022/7/15 13:25
 **/
public class TestController {
    public static void main(String[] args) {
//        addDict();
    List<String> openIDs =addDict();

            getDate(openIDs);
    }
    public static List<String> addDict() {
        List list = new ArrayList();


        EasyExcel.read("E:\\test.xlsx", ExcelDto.class, new AnalysisEventListener<ExcelDto>() {
                    //重写子类方法
                    @Override
                    public void invoke(ExcelDto question1, AnalysisContext analysisContext) {
                        list.add(question1);
                    }
                    //重写子类方法
                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                    }

                    @Override
                    public void invokeHeadMap(Map headMap, AnalysisContext context) {
                    }
                }
        ).doReadAll();

        List<ExcelDto> data = (List<ExcelDto>) list;
        List<String> opId  = new ArrayList<>();
        data.stream().forEach(t->{
           if(t.getCode() != null && !t.getCode().equals("NULL")){
               opId.add(t.getCode());
           }
        });


        return opId;

    }
    public static void getDate(List<String> openId){

        RestTemplate template = new RestTemplate();
        String url = "https://api.weixin.qq.com/cgi-bin/changeopenid?access_token=58_R8eowWbZhJ43Y6nBNwKX372jd_HXqluNC-Fx1mGxLhBAFTseF5L-N0G6_Jf09rF7rFfp3_xutsVOkbVJAGrbGxtzw3mfXWdts4MXNeBkqRuOyjUVABZ6QrPevX1wKvwUO4EXwA0jFvf4pcb7JHFiACAMKI";

        HttpHeaders headers = new HttpHeaders();
        JSONObject obj = new JSONObject();
//        obj.put("from_appid", "gh_15a3ee8a239a");
        obj.put("from_appid", "wx413366747556dc9c");
        obj.put("openid_list", openId);
        HttpEntity<JSONObject> httpEntity = new HttpEntity<JSONObject>(obj, headers);
        ResponseEntity<String> response = template.postForEntity(url, httpEntity, String.class);

        String json = response.getBody();
        JSONObject jsonDate = JSONObject.parseObject(json);

        String ss = jsonDate.getString("result_list");
        JSONArray jsonArray = JSON.parseArray(ss);
        int size = jsonArray.size();
        for (int i = 0; i < size; i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            System.out.println(jsonObject.getString("ori_openid")+"--"+jsonObject.getString("new_openid"));

        }
    }

}
